package com.fan.shop.views.bean;

import javax.el.ValueExpression;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;

import com.fan.shop.services.UserService;

@ManagedBean(name = "hello")
@RequestScoped
public class HelloBean {
	private String userName;
	private HtmlOutputText htmlOutputText;
	private int size;

	@ManagedProperty(value = "#{UserService}")
	UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public HelloBean() {
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String newValue) {
		size = userService.getAllUsers().size();

		this.userName = size + "";
	}

	public int getSize() {

		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public HtmlOutputText getHtmlOutputText() {
		return htmlOutputText;
	}

	public void setHtmlOutputText(HtmlOutputText newValue) {
		this.htmlOutputText = newValue;
	}

	public String sayHelloAction() {
		if (userName.equalsIgnoreCase("stranger")) {
			FacesContext context = FacesContext.getCurrentInstance();
			HtmlOutputText output = new HtmlOutputText();
			ValueExpression value = context
					.getApplication()
					.getExpressionFactory()
					.createValueExpression(context.getELContext(),
							"#{hello.htmlOutputText}", String.class);
			output.setValueExpression("value", value);

			output.setValue("New Friend!");
			output.setStyle("color:red");
			this.htmlOutputText = output;
			return "newFriend";
		} else {
			return "oldFriend";
		}
	}
}
